<?php

class CpanelController extends MyController
{
	function init()
	{
		parent::init();
		$controller_id = str_replace('controller','',strtolower(__CLASS__));
		$this->data['controller_id'] = $controller_id;
	}
	
	function actionIndex()
	{
		$who_online = session::model()->findAll(array("select"=>"user_id, time_online, ip_online"));
		$this->data["who_online"] = $who_online;
		$this->render("index", $this->data);
	}
	
	function actionView()
	{
		$who_online = session::model()->findAll(array("select"=>"user_id, time_online, ip_online"));
		
		$this->data["who_online"] = $who_online;
		
		$this->render("index", $this->data);	
	}
	
	function actionThongke()
	{
		$who_online = session::model()->findAll(array("select"=>"user_id, time_online, ip_online"));
		
		$this->data["who_online"] = $who_online;
		
		$this->render("view", $this->data);	
	}
	
	function actionList($page = 1)
	{
		$pages = new CPagination;
		$pages->pageSize = $this->data["website"]["beperpage"];
		
		$rows = cpanel::model()->findAll(array(
			"order" => "`order`, id ",
			"offset" => ($page-1)*$this->data["website"]["beperpage"],
			"limit" => $this->data["website"]["beperpage"]
		));
		
		$pages->itemCount =  cpanel::model()->count(); // tong so dong
		
		$this->data["rows"] = $rows;
		
		$this->data["pages"] = $pages;
		$this->data["page"] = $page;
		$this->render("view_list", $this->data);	
	}
	function actionAdd($page = 1)
	{
		$form = array();
		
		$form["id"] = 0;
		$form["cpanel_name"] = "";
		$form["cpanel_image"] = "";
		$form["controller_id"] = "";
		
		$this->data["form"] = $form;
		
		$this->data["page"] = $page;
		$this->render("form_edit", $this->data);
	}
	
	function actionEdit($cpanel_id = 0, $page = 1)
	{
		$row = cpanel::model()->findByPk($cpanel_id);
		
		$form = array();
		
		$form["id"] = $row->id;
		$form["cpanel_name"] = $row->cpanel_name;
		$form["cpanel_image"] = $row->cpanel_image;
		$form["controller_id"] = $row->controller_id;
		
		$this->data["form"] = $form;
		
		$this->data["page"] = $page;
		$this->render("form_edit", $this->data);
	}
	
	function actionSave($cpanel_id = 0, $page = 1)
	{
		$row = NULL;
		if ($cpanel_id == 0)
		{
			$row = new cpanel;
			
			$row->cpanel_name = Yii::app()->getRequest()->getPost("cpanel_name");
			$row->cpanel_image = Yii::app()->getRequest()->getPost("cpanel_image");
			$row->controller_id = Yii::app()->getRequest()->getPost("controller_id");
			
			$row->save();

		}
		else
		{
			$row = cpanel::model()->findByPk($cpanel_id);
			
			$row->cpanel_name = Yii::app()->getRequest()->getPost("cpanel_name");
			$row->cpanel_image = Yii::app()->getRequest()->getPost("cpanel_image");
			$row->controller_id = Yii::app()->getRequest()->getPost("controller_id");

			$row->save();
		}
		
		$this->redirect(array($this->data['controller_id'].'/list', "page"=>$page));
	}
	
	function actionEnable($cpanel_id = 0, $page = 1)
	{
		if ($cpanel_id == 0)
		{
			throw new CHttpException(404,'The specified post cannot be found.');
		}
		else
		{
			$cpanel = cpanel::model()->findByPk($cpanel_id);
			$cpanel->enable = 1 - $cpanel->enable;
			$cpanel->save();
			
			$this->redirect(array($this->data['controller_id'].'/list', "page"=>$page));
		}
	}
	
	function actionUp($cpanel_id = 0, $page = 1)
	{
		$cpanel = cpanel::model()->findByPk($cpanel_id);
		if ($cpanel->order > 1)
		{
			$cpanel_before = cpanel::model()->find(array("condition"=>"`order` = " . ($cpanel->order - 1)));
			if (! is_null($cpanel_before))
			{
				$cpanel_before->order ++;
				$cpanel_before->save();
			}
			$cpanel->order --;
			$cpanel->save();
		}
		
		$this->redirect(array($this->data['controller_id'].'/list', "page"=>$page));
	}
	
	function actionDown($cpanel_id = 0, $page = 1)
	{
		$cpanel = cpanel::model()->findByPk($cpanel_id);
		
		$cpanel_after = cpanel::model()->find(array("condition"=>"`order` = " . ($cpanel->order + 1)));
		if (! is_null($cpanel_after))
		{
			$cpanel_after->order --;
			$cpanel_after->save();
		}
			
		$cpanel->order ++;
		$cpanel->save();
		
		$this->redirect(array($this->data['controller_id'].'/list', "page"=>$page));
	}
	
	function actionReset_order( $page = 1 )
	{
		$cpanel_array = cpanel::model()->findAll();
		foreach($cpanel_array as $cpanel)
		{
			$cpanel->order = $cpanel->id;
			$cpanel->save();
		}
		
		$this->redirect(array($this->data['controller_id'].'/list', "page"=>$page));
	}
	
	function actionConfirm_delete($cpanel_id, $page = 1)
	{
		$row = cpanel::model()->findByPk($cpanel_id);
		$this->data['row'] = $row;
		
		$this->data["page"] = $page;
		$this->render("confirm_delete", $this->data);
	}
	
	function actionDelete($cpanel_id = 0 , $page = 1)
	{
		cpanel::model()->deleteByPk($cpanel_id);
		
		$rows = cpanel::model()->findAll(array(
			"offset" => ($page-1)*$this->data["website"]["beperpage"],
			"limit" => $this->data["website"]["beperpage"]
		));
		
		if (count($rows) == 0)
		{
			if ($page > 1)
				$page --;
		}
		
		$this->redirect(array($this->data['controller_id'].'/list', "page"=>$page));
	}
	
	function actionDelete_multi($page = 1)
	{
		//var_dump(Yii::app()->getRequest()->getPost("cib"));
		
		$id_array = Yii::app()->getRequest()->getPost("cib");
		if (!is_null($id_array))
		{
			foreach($id_array as $id=>$value)
				cpanel::model()->deleteByPk($id);
		}
		
		$rows = cpanel::model()->findAll(array(
			"offset" => ($page-1)*$this->data["website"]["beperpage"],
			"limit" => $this->data["website"]["beperpage"]
		));
		
		if (count($rows) == 0)
		{
			if ($page > 1)
				$page --;
		}
		
		$this->redirect(array($this->data['controller_id'].'/list', "page"=>$page));
	}
	
	function actionView_icon()
	{
		
		$this->render("view_icon", $this->data);
	}
	
	function actionDelete_icon()
	{
		$icon_file = Yii::app()->getRequest()->getPost("icon_file");
		
		unlink(WEB_ROOT . "/". ADMIN_FOLDER . "/". $this->data["website"]["icon_cpanel_folder"]."/".$icon_file);
		
		$this->redirect(array($this->data['controller_id'].'/view_icon'));
		
	}
	
	function actionUpload_icon()
	{
		// include mimes
		include_once("mimes.php");
		// file type allow
		$file_type_allow = explode("|", $this->data["website"]["icon_type"]);
		$file_type = array();
		$i = 1;
		foreach($file_type_allow as $key=>$value)
		{
			if (! is_array($mimes[$value]))
				$file_type[$i++] = $mimes[$value];
			else{
				foreach($mimes[$value] as $mime)
					$file_type[$i++] = $mime;
			}
		}
		
		$file_maxsize = $this->data["website"]["icon_size"] * 1024; //file_maxsize KB
		
		$file = CUploadedFile::getInstanceByName("file");
		
		if ( ! is_null($file))
		{
			if (array_search(strtolower($file->getType()), $file_type) !== false) // kieu file hop le
			{
				if ($file->getSize() <= $file_maxsize)
				{
					$file_name = str_replace(" ","_",strtolower(MyHelper::convert_no_sign($file->name)));
					$file->saveAs(WEB_ROOT."/".ADMIN_FOLDER."/".$this->data["website"]["icon_cpanel_folder"]."/".$file_name);
				}
			}
		}
		
		$this->redirect(array($this->data['controller_id'].'/view_icon'));	
	}
	
}